package com.duoketech.utils.upload;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.io.InputStream;
import java.util.UUID;

public class QiNiuUpload {

	private final static String accessKey = "b0PyM0GiK3GP2c4Udr519XirHYk9sYlacG4499rt";
	private final static String secretKey = "dizHTFt75PGbL8UV0DBraQrei7ex-lw8-wvuQRsA";
	private final static String bucket = "dk-travel";
//	private final static String DOMAIN = "http://pji33d96f.bkt.clouddn.com";
	private final static String DOMAIN = "http://img.duoketech.com";
//	private final static String accessKey = "ya_qHvLA9VsRS3UzkY88u9OHVfpjw9Hid0P6Ktxv";
//	private final static String secretKey = "ZuPYGZvji-OX29_UsLgCnv4BwTGKuXULhhjmPT7j";
//	private final static String bucket = "xthink-cms";
//	private final static String DOMAIN = "http://p9fk1i55u.bkt.clouddn.com";
	/**
	 * 上传七牛云存储服务器
	 * @param localFile
	 * @return
	 */
	public static String upload(String localFile) {
		Configuration cfg = new Configuration(Zone.autoZone());
		UploadManager uploadManager = new UploadManager(cfg);
		String key = null;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		String fileName="";
		try {
			Response response = uploadManager.put(localFile, key, upToken);
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			fileName = putRet.hash;
		} catch (QiniuException ex) {
			
		}
		
		return fileName;
	}


	public static String uploadToQiNiuFromNet(String imgUrl,String fileName){
		Configuration cfg = new Configuration(Zone.autoZone());
		Auth auth = Auth.create(accessKey, secretKey);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		FetchRet putret;
		String imagePath="";
		try {
			putret = bucketManager.fetch(imgUrl, bucket, fileName);
			System.out.println(putret.key);
			imagePath = putret.key;
			System.out.println("succeed upload image");
		} catch (QiniuException e) {
			e.printStackTrace();
		}
		return DOMAIN+"/"+imagePath;
	}


	public static String uploadByJfinal(InputStream path, String category) throws QiniuException {
		Auth auth = Auth.create(accessKey, secretKey);
		Configuration cfg = new Configuration(Zone.autoZone());
		UploadManager uploadManager = new UploadManager(cfg);
		String upToken = auth.uploadToken(bucket,null, 3600, new StringMap()
				.putNotEmpty("saveKey",category+UUID.randomUUID().toString()), true);
		try {
			Response response = uploadManager.put(path,null,upToken,null, null);
			//解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			return DOMAIN+"/"+putRet.key;
		} catch (QiniuException ex) {
			throw ex;
		}
	}
}
